﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface ICGDDAL : IDAL<CGD>
    {
        List<CGDModel> GetCGD(Func<CGDModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        bool CGDConfirm(Guid ID);
    }
    public class CGDDAL : BaseDAL<CGD>, ICGDDAL
    {
        public CGDDAL(IUnitOfWork context)
            : base(context)
        { }
        public List<CGDModel> GetCGD(Func<CGDModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from cgd in dataContext.CGD
                         join bm in dataContext.Departments on cgd.BMID equals bm.Id
                         join ry in dataContext.Users on cgd.RYID equals ry.Id
                         join gys in dataContext.ZL_GYS on cgd.GYSID equals gys.ID
                         join czry in dataContext.Users on cgd.CZRYID equals czry.Id
                         select new CGDModel
                         {
                             BH=cgd.BH,
                             ID = cgd.ID,
                             BMID = cgd.BMID,
                             BMMC = bm.Name,
                             RYID = cgd.RYID,
                             RYName = ry.Name,
                             GYSID = cgd.GYSID,
                             GYSMC = gys.MC,
                             GYSJC=gys.JIANCHENG,
                             ZT = cgd.ZT,
                             RQ = cgd.RQ,
                             CJSJ = cgd.CJSJ,
                             DHSJ=cgd.DHSJ,
                             CZRYID=cgd.CZRYID,
                             CZRYName=czry.Name
                         };
            result = result.Where(exp).AsQueryable();
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
            {
                pageIndex--;
                return result.Skip(Convert.ToInt32(pageIndex) * pageSize).Take(pageSize).ToList();
            }
        }
        
        public bool CGDConfirm(Guid ID)
        {
            bool result = false;
            dataContext.Connection.Open();
            using (var bs = dataContext.Connection.BeginTransaction())
            {
                try
                {
                    CGD item = dataContext.CGD.FirstOrDefault(t => t.ID == ID);
                    item.ZT = 1;
                    if (Save())
                    {
                        bs.Commit();
                        result = true;
                    }
                }
                catch
                {
                    bs.Rollback();
                    return false;
                }
                finally
                {
                    dataContext.Connection.Close();
                }
            }
            return result;
        }
    }
}
